/*	Copyright 2007 - Xavier Baro (xbaro@cvc.uab.cat)

	This file is part of eapmlib.

    Eapmlib is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or any 
	later version.

    Eapmlib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __DISSOCIATEDDIPOLE_H__
#define __DISSOCIATEDDIPOLE_H__

#include "EvolutiveLib.h"
#include "Feature.h"

namespace Evolutive {

	//! Defines the error function
	enum EVOLUTIVELIB_API DIPEVAL_METHOD {DIPEVAL_MEAN,DIPEVAL_SUM};

	//! Defines the class where a dipole is stored
	class EVOLUTIVELIB_API CDipoleStruct
	{
	public:
		CvRect Region;
		int Channel;
		int Weight;
		bool Rotated;
	};

	//! Class that defines a dissociated dipole
	class EVOLUTIVELIB_API CDissociatedDipole : public CFeature
	{
		//! Methods
	public:
		//! Default constructor		
		CDissociatedDipole(void);

		//! Default destructor
		virtual ~CDissociatedDipole(void);

#ifdef USE_OPENCV
		//! Returns the class name (From base class)
		virtual string GetClassNameID(void) {return "CDissociatedDipole";}

		//! Sets the feature parameters (From base class)
		virtual void SetParameters(int NumParameters, double *Parameters);

		//! Verify if a feature is a valid feature (From base class)
		virtual bool IsValid(CvSize WinSize,bool UseColor);

		//! Apply the feature to an image object (From base class)
		virtual double Apply(CData *InputData);

		//! Apply the feature to an image object
		double Apply(IplImage *IntImage,IplImage *RIntImage,int Offset=0,double Scale=1.0);

		//! Draw the feature over a given image
		void DrawFeature(IplImage *Image);

		//! Set the minimum dipole area
		void SetMinArea(int Value);		

		//! Sets the evaluation type
		void SetEvalType(DIPEVAL_METHOD Value);

		//! Returns the feature data
		CDipoleStruct* GetData(int &NumRegions);

		//! Obtain the number of parameters that defines the feature (From base class)
		virtual int GetNumParameters(void);

		//! Obtain a pointer to the parameters (From base class)
		virtual void GetParameters(double *Parameters);

		//! Save or recover the data to/from a file (From base class)
		virtual void Serialize(std::fstream &fs,bool Saving);

	private:
		//! Generate a dipole from Haar-like feature parameters
		void GetDipoleFromHaar(double *ParsIN,unsigned int *ParsOUT);
#endif //USE_OPENCV
							
		//! Attributes
	private:		

		//! Dissociate dipole definition
		CDipoleStruct m_Dipole[2];		

		//! Minimum dipole area
		int m_MinArea;		

		//! Number of regions
		int m_NumRegions;

		//! Method to evaluate the dipole value
		DIPEVAL_METHOD m_EvaluationType;
	};	
}

#endif // __DISSOCIATEDDIPOLE_H__
